import { Application } from 'egg';
import {
  indexRule,
  createRule,
  updateRule,
  destroyRule,
 } from '../validate/shop/notice';

/**
 * 公告管理
 */
export default (app: Application) => {
  const { auth } = app.middleware;
  const {  jwt } = app;

  return {
    'merchant.notice.list': {
      method: 'GET',
      path: '/api/merchant/notice',
      options: {
        tags: ['type/list'],
        description: '公告列表',
        handler: app.controller.shop.notice.list,
        middlewares: [jwt, auth('merchant.notice.list')],
        validate: indexRule,
      },
    },
    'merchant.notice.create': {
      method: 'POST',
      path: '/api/merchant/notice',
      options: {
        tags: ['type/create'],
        description: '创建公告',
        handler: app.controller.shop.notice.create,
        middlewares: [jwt, auth('merchant.notice.create')],
        validate: createRule,
      },
    },
    'merchant.notice.update': {
      method: 'PUT',
      path: '/api/merchant/notice/:id',
      options: {
        tags: ['type/update'],
        description: '更新公告',
        handler: app.controller.shop.notice.update,
        middlewares: [jwt, auth('merchant.notice.update')],
        validate: updateRule,
      },
    },
    'merchant.notice.destroy': {
      method: 'DELETE',
      path: '/api/merchant/notice/:id',
      options: {
        tags: ['type/delete'],
        description: '删除公告',
        handler: app.controller.shop.notice.destroy,
        middlewares: [jwt, auth('merchant.notice.destroy')],
        validate: destroyRule,
      },
    }
  };
};
